//
//  EyuAdBridge.m
//  QySDKBridge
//
//  Created by lcc on 2025/2/10.
//

#import "EyuAdBridge.h"
#import "AdEventType.h"
#import <EyuGameSDK/AboGameSDKApi.h>
#import <QySDKBridge/SDKBridgeUtil.h>
#import "EyuHeader.h"
#import "QyBaseEvent.h"
#import "SDKBridgeConstant.h"
#import "EyuReportBridge.h"
#import "UIKit/UIKit.h"
@implementation EyuAdBridge

+ (BOOL)IsAdLoad:(int)type{
    BOOL result = false;
    if (type == INTERSTITIAL){
        result = [[AboGameSDKApi SDKManager] InterstitialAdIsLoaded];
    }
    else if (type == REWARDED){
        result = [[AboGameSDKApi SDKManager] RewardAdIsLoaded];
    }
    return result;
}

+ (BOOL)ShowAd:(int)type{
    BOOL result = true;
    if (type == BANNER){
        [EyuAdBridge ShowBannerAd];
    }
    else if (type == INTERSTITIAL){
        [EyuAdBridge ShowInterAd];
    }
    else if (type == REWARDED){
        [EyuAdBridge ShowRewardAd];
    }
    
    return result;
}

+ (void)HideAd:(int)type{
    NSLog(@"--->>> HideAd - type:%d", type);
    if (type == BANNER){
        NSLog(@"--->>> HideAd - will close banner ad.");
        [[AboGameSDKApi SDKManager] GameRemoveBannerView];
    }
}

+ (void)SendToUnity:(NSString *)adName adKey:(NSString *)adkey eventType:(int)eventType{
    NSString * json = [SDKBridgeUtil ObjectToJson:@{
        @"adType":[NSNumber numberWithInteger:REWARDED],
        @"unitName":adName,
        @"unitKey":adkey,
        @"eventType":[NSNumber numberWithInt:eventType]
    }];
    NSLog(@"--->>> AdBridge - json:%@", json);
    [SDKBridgeUtil SendMsgToUnity:UnitySdkAdStateFunc msg:json];
}

+ (void)ShowRewardAd{
    [[AboGameSDKApi SDKManager] GameLoadRewardAdWithShow:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"开始播放激励广告 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:SHOW_SUCCESS];
    } LookFinish:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"激励播放完成，发放奖励 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:ON_REWARDED];
    } userClick:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"用户点击激励广告 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:ON_CLICK];
    } adLoadFail:^(AboAdParams * _Nonnull adUnit, NSError * _Nonnull error) {
        NSString * output = [NSString stringWithFormat:@"激励广告加载失败 - adUnit - name:%@,type:%@,key:%@，error:%@", adUnit.name, adUnit.type, adUnit.key, error];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:LOAD_FAIL];
    }];
}

+ (void)ShowInterAd{
    [[AboGameSDKApi SDKManager] GameLoadInterstitialAdWithShow:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"开始播放插屏广告 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:SHOW_SUCCESS];
    } hide:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"插屏广告隐藏 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:ON_CLOSE];
    } userClick:^(AboAdParams * _Nonnull adUnit) {
        NSString * output = [NSString stringWithFormat:@"用户点击插屏广告 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:ON_CLICK];
    } adLoadFail:^(AboAdParams * _Nonnull adUnit, NSError * _Nonnull error) {
        NSString * output = [NSString stringWithFormat:@"插屏广告加载失败 - adUnit - name:%@,type:%@,key:%@", adUnit.name, adUnit.type, adUnit.key];
        NSLog(@"%@", output);
        [EyuAdBridge SendToUnity:adUnit.name adKey:adUnit.key eventType:LOAD_FAIL];
    }];
}

+ (void)ShowBannerAd{
    CGFloat screenHeight = CGRectGetHeight(UIScreen.mainScreen.bounds);
    CGFloat bottomHeight = [EyuAdBridge BottomHeight];
    CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds);
    CGFloat height = (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) ? 90 : 50;
    [[AboGameSDKApi SDKManager] GameLoadBannerAdWithFrame:CGRectMake(0, screenHeight-height-bottomHeight, width, height) backColor:[UIColor clearColor] expand:^(AboAdParams * _Nonnull adUnit) {
        NSLog(@"Banner广告放大 %@--%@", adUnit.key, adUnit.name);
    } collapse:^(AboAdParams * _Nonnull adUnit) {
        NSLog(@"Banner广告缩小 %@--%@", adUnit.key, adUnit.name);
    }];
}

+(CGFloat)BottomHeight{
    if (@available(iOS 11.0, *)) {
        UIWindow *window = nil;
        if (@available(iOS 13.0, *)) {
            NSSet *set = [UIApplication sharedApplication].connectedScenes;
            UIWindowScene *windowScene = [set anyObject];
            window = windowScene.windows.firstObject;
        }else{
            window=UIApplication.sharedApplication.keyWindow;
        }
        return window.safeAreaInsets.bottom;
    } else {
        return 0;
    }
}

@end
